//
//  PostDetailTableViewCell_header.m
//  SXHomeTownClub
//
//  Created by 周思扬 on 2018/5/29.
//  Copyright © 2018年 jsx. All rights reserved.
//

#import "PostDetailTableViewCell_header.h"

#define TextWidth (SDScreenWidth-20)

@implementation PostDetailTableViewCell_header

- (void)awakeFromNib {
    [super awakeFromNib];
    self.focusBtn.layer.cornerRadius=5;
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickUserHead)];
    [self.headImg setUserInteractionEnabled:YES];
    [self.headImg addGestureRecognizer:tap];
    [[self.focusBtn rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIControl * _Nullable x){
        if(self->_focusblock)
        {
            self->_focusblock();
        }
    }];
}

-(void)clickUserHead
{
    if(_userblock)
    {
        _userblock();
    }
}

-(void)clickImg:(UITapGestureRecognizer*)tap
{
    NSNumber* index = objc_getAssociatedObject(tap,"index");
    if(_block)
    {
        _block(index.intValue);
    }
}

-(void)setContentData:(PostDetailData *)contentData
{
    float height=0.0;
    if(_contentData==nil)
    {
        NSMutableArray * tempImgArr=[NSMutableArray array];
        for(int i=0;i<contentData.wen.count;i++)
        {
            PostDetailData_article * article=contentData.wen[i];
            if([article.type isEqualToString:@"TEXT"])
            {
                UILabel * textView=[[UILabel alloc]initWithFrame:CGRectMake(0, height+5, TextWidth,[article.content heightForWidth:TextWidth])];
                textView.numberOfLines=0;
                NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:article.content attributes:self.typingAttrDict];
                textView.attributedText=attributedText;
                [self.contentBgView addSubview:textView];
                height+=[article.content heightForWidth:TextWidth]+15;
            }else if([article.type isEqualToString:@"IMAGE"])
            {
                UIImage * placeimage=[UIImage imageNamed:PlaceHolderImg_Post];
                UIImage * image=[UIImage imageWithData:[[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:article.content]]];
                UIImageView * imgv=[[UIImageView alloc]init];
                imgv.frame=CGRectMake(0, height+10, TextWidth, TextWidth*(image.size.height/image.size.width)+10);
                [imgv sd_setImageWithURL:[NSURL URLWithString:article.content] placeholderImage:placeimage];
                [self.contentBgView addSubview:imgv];
                height+=TextWidth*(image.size.height/image.size.width)+20;
                
                [imgv setUserInteractionEnabled:YES];
                UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImg:)];
                [tempImgArr addObject:image];
                objc_setAssociatedObject(tap,"index",[NSNumber numberWithInt:(int)tempImgArr.count],OBJC_ASSOCIATION_RETAIN_NONATOMIC);
                [imgv addGestureRecognizer:tap];
            }
        }
        
        self.contentBgViewHeight.constant=height;
    }
    
    for(int i=0;i<self.zanView.subviews.count;i++)
    {
        if([self.zanView.subviews[i] isKindOfClass:UIImageView.class])
        {
            UIImageView * img=self.zanView.subviews[i];
            [img removeFromSuperview];
        }
    }
    float imgheight=0.0;
    int hangNum=TextWidth/40;
    int hang=0;
    if(contentData.zanlist.count>0)
    {
        hang=((int)contentData.zanlist.count-1)/hangNum+1;
    }
    imgheight=hang*40;
    for(int i=0;i<contentData.zanlist.count;i++)
    {
        float x=5+40*(i%hangNum);
        float y=5+40*(i/hangNum);
        PostDetailData_user *user=contentData.zanlist[i];
        UIImageView * userImg=[[UIImageView alloc]init];
        userImg.layer.masksToBounds=YES;
        userImg.layer.cornerRadius=15;
        [userImg sd_setImageWithURL:[NSURL URLWithString:user.headimg] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
        userImg.frame=CGRectMake(x, y, 30, 30);
        [self.zanView addSubview:userImg];
    }
    self.zanViewHeight.constant=imgheight;
    
    self.subjectLab.text=contentData.subject;
    [self.nameBtn setTitle:contentData.username forState:UIControlStateNormal];
    self.levelLab.text=[NSString stringWithFormat:@"LV%@",contentData.level];
    self.timeLab.text=contentData.addtime;
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:contentData.headimg] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Head]];
    if(contentData.plcont.length==0||contentData.views.length==0)
    {
        self.readNumLab.text=[NSString stringWithFormat:@"评论0 阅读0"];
    }else
    {
        self.readNumLab.text=[NSString stringWithFormat:@"评论%@ 阅读%@",contentData.plcont,contentData.views];
    }
    
    if([contentData.guanstatus isEqualToString:@"0"])
    {
        self.focusBtn.hidden=NO;
    }else
    {
        self.focusBtn.hidden=YES;
    }
    
    _contentData=contentData;
}

-(NSDictionary *)typingAttrDict
{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    // 字体的行间距
    paragraphStyle.lineSpacing = 5;
    //首行缩进
    paragraphStyle.firstLineHeadIndent = 28;
    //整体缩进(首行除外)
    paragraphStyle.headIndent = 0;
    NSDictionary *attributes = @{NSFontAttributeName:SDFontOf14,NSForegroundColorAttributeName:TextColor,NSParagraphStyleAttributeName:paragraphStyle};
    return attributes;
}

@end
